package com.lhy.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhy.domain.Classify;
import com.lhy.domain.Product;
import com.lhy.domain.ProductDetails;
import com.lhy.domain.Spec;
import com.lhy.goods.mapper.ClassifyMapper;
import com.lhy.goods.mapper.ProductDetailsMapper;
import com.lhy.goods.mapper.ProductMapper;
import com.lhy.goods.mapper.SpecMapper;
import com.lhy.goods.service.ProductDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lhy
 * @description 针对表【goods_product_details(商品详情表)】的数据库操作Service实现
 * @createDate 2024-07-30 12:13:09
 */
@Service
public class ProductDetailsServiceImpl implements ProductDetailsService {
    @Autowired
    private ProductDetailsMapper iProductDetailsMapper;
    @Autowired
    private ProductMapper iProductMapper;
    @Autowired
    private SpecMapper iSpecMapper;
    @Autowired
    private ClassifyMapper iClassifyMapper;

    @Override
    public List<ProductDetails> selectAll() {
        LambdaQueryWrapper<ProductDetails> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductDetails::getIsDelete, 0);
        return iProductDetailsMapper.selectList(lqw);
    }


    @Override
    public List<ProductDetails> selectList(ProductDetails iProductDetails) {
        LambdaQueryWrapper<ProductDetails> lqw = this.lqw(iProductDetails);
        return iProductDetailsMapper.selectList(lqw);
    }

    @Override
    public Integer insert(ProductDetails iProductDetails) {
        LambdaQueryWrapper<ProductDetails> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductDetails::getProductId,iProductDetails.getProductId());
        lqw.eq(ProductDetails::getIsDelete,0);
        lqw.orderByAsc(ProductDetails::getPrice);
        List<ProductDetails> list = iProductDetailsMapper.selectList(lqw);
        Product product=new Product();
        product.setId(iProductDetails.getProductId());
        if (!list.isEmpty()) {
            if (iProductDetails.getPrice().compareTo(list.get(0).getPrice())>0){
                product.setPrice(list.get(0).getPrice());
            }else {
                product.setPrice(iProductDetails.getPrice());
            }
        }else {
            product.setPrice(iProductDetails.getPrice());
        }
        iProductMapper.updateById(product);
        return iProductDetailsMapper.insert(iProductDetails);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                ProductDetails iProductDetails = new ProductDetails();
                iProductDetails.setId(Long.parseLong(id));
                iProductDetails.setIsDelete(1);
                iProductDetailsMapper.updateById(iProductDetails);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(ProductDetails iProductDetails) {
        return iProductDetailsMapper.updateById(iProductDetails);
    }

    @Override
    public Page<ProductDetails> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<ProductDetails>) this.selectAll();
    }

    @Override
    public Page<ProductDetails> pageList(ProductDetails iProductDetails, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<ProductDetails>) this.selectList(iProductDetails);
    }

    @Override
    public Map<String, ?> findDetail(Long id) {
        Map base = new HashMap<>();
        Map trade = new HashMap<>();
        Map specFile = new HashMap<>();
        Map result = new HashMap<>();
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getId, id).eq(Product::getIsDelete, 0);
        Product product = iProductMapper.selectOne(productLambdaQueryWrapper);
        Classify classify = this.getClassify(Long.valueOf(product.getClassifyId()));
        StringBuilder classifyName = new StringBuilder();
        if (classify != null) {
            classifyName.insert(0, classify.getClassifyName());
            if (classify.getIsuperiorId() != 0) {
                Classify superior = this.getClassify(classify.getIsuperiorId());
                classifyName.insert(0, superior.getClassifyName() + ">");
                if (superior.getIsuperiorId() != 0) {
                    superior = this.getClassify(classify.getIsuperiorId());
                    classifyName.insert(0, superior.getClassifyName() + ">");
                }
            }
        }
        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(id);
        productDetails.setIsDelete(0);
        List<ProductDetails> productDetails1 = this.selectList(productDetails);
        for (ProductDetails productDetail : productDetails1) {
            String specId = productDetail.getSpecId();
            StringBuilder name = new StringBuilder();
            String[] split = specId.split(",");
            int count = 0;
            for (String specId1 : split) {
                Spec spec = iSpecMapper.selectById(Long.valueOf(specId));
                if (count==0){
                    name.append(spec);
                    count++;
                }
                name.append(" ").append(spec);
            }
            productDetails.setSpecId(name.toString());
        }
        //基本信息
        base.put("商品分类", classifyName);
        base.put("商品名称", product.getProductName());
        base.put("商品卖点", product.getSelling());
        base.put("商品参数", product.getParameter());
        //商品交易信息
        trade.put("计量单位", product.getUnitMeasure());
        trade.put("销售模式", product.getSalesModel());
        //商品规格及图片
        specFile.put("商品编号", product.getId());
        specFile.put("商品价格", product.getPrice());
        specFile.put("商品图片", product.getImg());
        specFile.put("商品视频", product.getVideo());
        specFile.put("商品规格", productDetails1);
        //返回值
        result.put("基本信息", base);
        result.put("商品交易信息", trade);
        result.put("商品规格及图片", specFile);
        result.put("商品详情描述", product.getDescribed());
        return result;
    }

    private Classify getClassify(Long id) {
        LambdaQueryWrapper<Classify> classifyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classifyLambdaQueryWrapper.eq(Classify::getId, id).eq(Classify::getIsDelete, 0);
        return iClassifyMapper.selectOne(classifyLambdaQueryWrapper);
    }

    private LambdaQueryWrapper lqw(ProductDetails iProductDetails) {
        LambdaQueryWrapper<ProductDetails> lqw = new LambdaQueryWrapper<>();
        if (iProductDetails.getId() != null) {
            lqw.eq(ProductDetails::getId, iProductDetails.getId());
        }
        if (iProductDetails.getSpecId() != null) {
            lqw.eq(ProductDetails::getSpecId, iProductDetails.getSpecId());
        }
        if (iProductDetails.getNum() != null) {
            lqw.eq(ProductDetails::getNum, iProductDetails.getNum());
        }
        if (iProductDetails.getWeight() != null) {
            lqw.eq(ProductDetails::getWeight, iProductDetails.getWeight());
        }
        if (iProductDetails.getPrice() != null) {
            lqw.eq(ProductDetails::getPrice, iProductDetails.getPrice());
        }
        if (iProductDetails.getInventory() != null) {
            lqw.eq(ProductDetails::getInventory, iProductDetails.getInventory());
        }
        if (iProductDetails.getImg() != null) {
            lqw.eq(ProductDetails::getImg, iProductDetails.getImg());
        }
        if (iProductDetails.getProductId() != null) {
            lqw.eq(ProductDetails::getProductId, iProductDetails.getProductId());
        }
        if (iProductDetails.getIsDelete() != null) {
            lqw.eq(ProductDetails::getIsDelete, iProductDetails.getIsDelete());
        }
        if (iProductDetails.getCreateTime() != null) {
            lqw.eq(ProductDetails::getCreateTime, iProductDetails.getCreateTime());
        }
        if (iProductDetails.getUpdateTime() != null) {
            lqw.eq(ProductDetails::getUpdateTime, iProductDetails.getUpdateTime());
        }
        return lqw;
    }
}




